<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2023-02-15 11:41:14
 * @LastEditors: Sam
 * @LastEditTime: 2023-02-15 17:05:48
 */

namespace app\common\support;



use Nacos\Models\ServiceInstance;
use Nacos\NacosClient;
use Nacos\Models\BeatInfo;
use Nacos\Models\BeatResult;
use Nacos\NacosNaming;
use Nacos\Utils\RandomByWeightSelector;
use think\facade\Cache;
use think\Config;
use think\exception\ErrorException;
use think\facade\Log;
use think\facade\Env;

class Nacos{

    private static $instance;
    private $host; //nacos地址
    private $port; //nacos端口

    private function __construct()
    {
        $this->host = Env::get('nacos.host');
        $this->port = Env::get('nacos.port');
    }


    public static function get_instance()
    {
        if (!self::$instance instanceof self) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    public function getBaseConfig($key){

        $client = new NacosClient($this->host, $this->port);
        $namespace_id = Env::get('nacos.base_namespace_id');
        $client->setNamespace($namespace_id); //设置命名空间
        $dataId       =  Env::get('nacos.base_data_id');
        $group        = Env::get('nacos.base_group');
        try{
            $configs = $client->getConfig($dataId,$group);
            if(!empty($configs)){
//                $configs = \GuzzleHttp\json_decode($configs, true);
                $configs = json_decode($configs,true);
                $value = isset($configs[$key]) ? $configs[$key] : "";
            }
            return $value;
        }catch (\Exception $e){
            $data = [
                'Time'    => date('Y-m-d H-i-s',time()),
                'Message' => $e->getMessage(),
                'File'    => $e->getFile(),
                'Line'    => $e->getLine()
            ];
            json_response(0,$data);
        }
    }
    
    /**
     * 获取nacos配置参数
     * @param $key
     * @return string
     */
    public function getConfig($key){

        $client = new NacosClient($this->host, $this->port);
        $namespace_id = Env::get('nacos.namespace_id');
        $client->setNamespace($namespace_id); //设置命名空间
        $dataId       =  Env::get('nacos.data_id');
        $group        = Env::get('nacos.group');
        try{
            $configs = $client->getConfig($dataId,$group);
            if(!empty($configs)){
//                $configs = \GuzzleHttp\json_decode($configs, true);
                $configs = json_decode($configs,true);
                $value = isset($configs[$key]) ? $configs[$key] : "";
            }
            // $this->listenConfig();
            return $value;
        }catch (\Exception $e){
            $data = [
                'Time'    => date('Y-m-d H-i-s',time()),
                'Message' => $e->getMessage(),
                'File'    => $e->getFile(),
                'Line'    => $e->getLine()
            ];
            json_response(0,$data);
        }
    }

    public function listenConfig(){
        $client = new NacosClient($this->host, $this->port);
        $namespace_id =  Env::get('nacos.namespace_id');
        $client->setNamespace($namespace_id); //设置命名空间
        $dataId = Env::get('nacos.data_id');
        $group   = Env::get('nacos.group');
        $configs = $client->getConfig($dataId,$group);
        $contentMd5 = md5($configs);
        $cache = new Config();
        $cache->dataId = $dataId;
        $cache->group = $group;
        $cache->contentMd5 = $contentMd5;
        $cache->namespace = $namespace_id;
        $result = $client->listenConfig([$cache]);
    }


}
